package api

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"go-zero-demo/user-api/internal/svc"
	"go-zero-demo/user-api/internal/types"
	"go-zero-demo/user-rpc/pb"
)

type CreateUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
	return &CreateUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateUserLogic) CreateUser(req *types.CreateUserReq) (resp *types.CreateUserResp, err error) {
	// builder po
	insertUser := new(pb.CreateUserReq)
	insertUser.Sex = req.Sex
	insertUser.Username = req.UserName
	insertUser.Password = req.Password
	insertUser.Nickname = req.NickName
	insertUser.Address = req.Address
	insertUser.HeadUrl = req.HeadUrl
	insertUser.Desc = req.Desc
	insertUser.Mobile = req.Mobile
	insertUser.RoleIdArr = req.RoleIdArr
	insertUser.Birthday = req.Birthday
	user, err := l.svcCtx.UserCenterClient.CreateUser(l.ctx, insertUser)
	if err != nil {
		return nil, err
	}
	// 3.return
	returnResp := new(types.CreateUserResp)
	returnResp.UserId = user.UserId
	return returnResp, nil
}
